// 模拟后台API返回的路由配置
export const fetchRoutesFromBackend = async () => {
  // 实际项目中这里应该是真实的API调用
  // const response = await fetch('/api/routes');
  // return response.json();

  // 模拟API延迟和返回数据
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        routes: [
          {
            path: "/",
            component: "Home",
            title: "首页",
            exact: true,
          },
          {
            path: "/about",
            component: "About",
            title: "关于我们",
          },
          {
            path: "/app",
            component: "App",
            title: "应用",
          },
        ],
      });
    }, 1000); // 模拟1秒延迟
  });
};

// 本地缓存路由配置
let cachedRoutes = null;

export const getRoutes = async () => {
  if (cachedRoutes) {
    return cachedRoutes;
  }

  try {
    const data = await fetchRoutesFromBackend();
    cachedRoutes = data.routes;
    return cachedRoutes;
  } catch (error) {
    console.error("获取路由配置失败:", error);
    // 返回默认路由配置
    return [
      {
        path: "/",
        component: "Home",
        title: "首页",
        exact: true,
      },
      {
        path: "/app",
        component: "App",
        title: "应用",
      },
    ];
  }
};

// 清除缓存
export const clearRoutesCache = () => {
  cachedRoutes = null;
};
